<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>医院管理</el-breadcrumb-item>
      <el-breadcrumb-item>医生管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item style="width: 400px">
          <el-col :span="7">
            <el-select
              v-model="queryInfo.query.time_type"
              placeholder="请选择"
              size="mini"
            >
              <el-option label="创建时间" value="1"></el-option>
              <el-option label="认证时间" value="2"></el-option>
            </el-select>
          </el-col>
          <el-col :span="8">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.start_time"
              size="mini"
              style="width: 100%"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="1" style="text-align: center">-</el-col>
          <el-col :span="8">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.end_time"
              size="mini"
              style="width: 100%"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="医生姓名【ID】" prop="doctor_id">
          <el-select
            v-model="queryInfo.query.doctor_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.get_doctor"
              :key="item.id"
              :label="item.real_name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 所属医院 -->
        <el-form-item label="所属医院【ID】" prop="hospital_id">
          <el-select v-model="queryInfo.query.hospital_id" filterable clearable>
            <el-option
              v-for="item in selectData.get_hospital"
              :key="item.id"
              :label="item.title + '【' + item.id + '】'"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryInfo.query.status" filterable clearable>
            <el-option key="0" label="待审核" value="0"></el-option>
            <el-option key="1" label="认证通过" value="1"></el-option>
            <el-option key="2" label="认证不通过" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <!-- 导出数据 -->
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>

      <!-- 列表区域 -->
      <el-table :data="list" border stripe>
        <el-table-column label="医生姓名【ID】" min-width="110px" prop="code">
          <template slot-scope="scope">
            {{ scope.row.real_name }}【{{ scope.row.id }}】
          </template>
        </el-table-column>
        <el-table-column
          label="所属医院【ID】"
          min-width="200px"
          prop="servcie_name"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.hospital_id == 0"></span>
            <span v-else
              >{{ scope.row.title }}【{{ scope.row.hospital_id }}】</span
            >
          </template>
        </el-table-column>
        <el-table-column
          label="性别"
          min-width="60px"
          prop="sexName"
        ></el-table-column>
        <el-table-column
          label="身份证号"
          min-width="120px"
          prop="idcard"
        ></el-table-column>
        <el-table-column label="职称|擅长专科" min-width="200px">
          <template slot-scope="scope">
            <span> {{ scope.row.job_title }}</span>
            <br />
            <span>{{ scope.row.good_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="毕业学校|专业" min-width="120px">
          <template slot-scope="scope">
            <span> {{ scope.row.university }}</span> |
            <span>{{ scope.row.major }}</span>
          </template>
        </el-table-column>
        <el-table-column label="毕业年份|工作年限" min-width="130px">
          <template slot-scope="scope">
            <span> {{ scope.row.graduation_year }}</span
            >年 | <span>{{ scope.row.working }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          min-width="90px"
          prop="statusName"
        ></el-table-column>
        <el-table-column label="认证时间|创建时间" min-width="140px">
          <template slot-scope="scope">
            <span v-if="scope.row.audit_time != null">
              {{ scope.row.audit_time }}</span
            >
            <span v-else>-</span>
            <br />
            <span>{{ scope.row.create_time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100px" fixed="right">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="delayDialogVisibles(scope.row)"
              v-if="scope.row.audit_status == 0"
              >审核</el-link
            >
            &nbsp;
            <el-link
              type="primary"
              @click="lookInfo(scope.row)"
              v-if="scope.row.audit_status == 1 || scope.row.audit_status == 2"
              >查看详情</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 查看详情操作对话框 -->
    <el-dialog
      :visible.sync="delayDialogVisible"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
    >
      <el-card>
        <el-descriptions :column="2" :labelStyle="LABEL">
          <el-descriptions-item label="医生姓名">{{
            examineData.real_name
          }}</el-descriptions-item>
          <el-descriptions-item label="所属医院">{{
            examineData.title
          }}</el-descriptions-item>
          <el-descriptions-item label="性别">{{
            examineData.sexName
          }}</el-descriptions-item>
          <el-descriptions-item label="身份证号">{{
            examineData.idcard
          }}</el-descriptions-item>
          <el-descriptions-item label="毕业学校">{{
            examineData.university
          }}</el-descriptions-item>
          <el-descriptions-item label="专业">{{
            examineData.major
          }}</el-descriptions-item>
          <el-descriptions-item label="毕业年份"
            >{{ examineData.graduation_year }}年</el-descriptions-item
          >
          <el-descriptions-item label="工作年限" v-if="">
            {{ examineData.working }}
          </el-descriptions-item>
          <el-descriptions-item label="职称">{{
            examineData.job_title
          }}</el-descriptions-item>
          <el-descriptions-item label="擅长专科">{{
            examineData.good_at
          }}</el-descriptions-item>
          <el-descriptions-item label="擅长领域" v-if="examineData.good_area == '' || examineData.good_area == 'undefined'">
            -
          </el-descriptions-item>
          <el-descriptions-item label="擅长领域" v-else>{{
            examineData.good_area
          }}</el-descriptions-item>
          <el-descriptions-item label="个人简介" v-if="examineData.info == '' || examineData.info == 'undefined'">
            -
          </el-descriptions-item>
          <el-descriptions-item label="个人简介" v-else>{{
            examineData.info
          }}</el-descriptions-item>
          <el-descriptions-item label="状态">{{
            examineData.statusName
          }}</el-descriptions-item>
          <el-descriptions-item label="认证时间">{{
            examineData.audit_time
          }}</el-descriptions-item>
          <el-descriptions-item label="审核意见" span="2" v-if="examineData.audit_info == ''">
            -
          </el-descriptions-item>
          <el-descriptions-item label="审核意见" span="2" v-else>{{
            examineData.audit_info
          }}</el-descriptions-item>
          <el-descriptions-item label="资格证">{{
            examineData.doctor_licence_no
          }}</el-descriptions-item>
        </el-descriptions>

        <div class="demo-image__preview">
          <el-image
            style="width: 100px; height: 100px"
            :src="examineData.doctor_licence"
            :preview-src-list="bigImg"
          >
          </el-image>
        </div>
      </el-card>
      <el-card v-show="this.is_examine">
        <el-form label-width="70px" :model="examineData">
          <el-form-item label="审批意见" prop="examineInfo">
            <el-input
              type="textarea"
              placeholder="审核拒绝必填"
              v-model="examineData.examineInfo"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-card>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer" v-show="this.is_examine">
        <el-button @click="examineDoctor(2)">审批拒绝</el-button>
        <el-button class="theme-background-color" @click="examineDoctor(1)"
          >审批通过</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          time_type: '1',
          start_time: '',
          end_time: '',
          project_id: '',
          gift_id: '',
          service_id: '',
          send_user: '',
          key_words: ''
        },
        page: 1,
        size: 20
      },
      selectData: [],
      list: [],
      total: 0,

      btnLoading: false,
      examineData: [],
      delayDialogVisible: false,
      is_examine: false,
      title: '审核',
      LABEL: {
        justifyContent: 'flex-end',
        minWidth: '70PX',
        color: '#999'
      },
      bigImg: []
    }
  },
  created() {
    this.initTime()
    this.getSelectData()
    this.getListData()
  },
  methods: {
    // 初始化日期
    initTime() {
      const endDate = this.getFormatDate(new Date()).substr(0, 11)
      const beginDate = this.getFormatDate(
        new Date(new Date() - 3600 * 1000 * 24 * 29)
      ).substr(0, 11)
      this.queryInfo.query.start_time = beginDate
      this.queryInfo.query.end_time = endDate
    },
    async getSelectData() {
      const url = 'admin/Data/find'
      const { data: res } = await this.$http.post(url, {
        code: 'get_doctor,get_hospital'
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    searchList() {
      this.queryInfo.page = 1
      this.getListData()
    },
    resetSearchForm() {
      this.initTime()
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 表格数据
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/Doctor/doctorList',
        this.queryInfo
      )
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.size = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.page = newPage
      this.getListData()
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/Doctor/downloadExcel',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '医生数据.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          console.log(err)
          this.$message.error('下载失败')
        })
    },
    async delayDialogVisibles(row) {
      this.delayDialogVisible = true
      this.is_examine = true
      this.examineData = row
      this.examineData.examine_info = ''
      this.bigImg = [row.doctor_licence]
      this.title = '审核'
    },
    async lookInfo(row) {
      // 查看详情
      this.delayDialogVisible = true
      this.is_examine = false
      this.examineData = row
      this.examineData.examine_info = ''
      this.bigImg = [row.doctor_licence]
      this.title = '查看详情'
    },
    async examineDoctor(val) {
      if (
        val === 2 &&
        (this.examineData.examineInfo === undefined ||
          this.examineData.examineInfo === '')
      ) {
        return this.$message.error('拒绝通过必须输入拒绝原因')
      }
      const url = '/admin/Doctor/examineDoctor'
      const param = {
        id: this.examineData.id,
        status: val,
        examine_info: this.examineData.examineInfo,
        user_id: this.examineData.bid
      }
      const { data: res } = await this.$http.post(url, param)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.delayDialogVisible = false
      this.is_examine = false
      this.getListData()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
